//
//  MyAddressViewController.m
//  reSearchDemo
//
//  Created by helfy  on 15-4-15.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "MyAddressViewController.h"
#import "YMParameterCellObj.h"
#import "MyAddressEditViewController.h"
@interface MyAddressViewController ()
{
    NSMutableArray *listArray;
}
@end

@implementation MyAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title =@"我的地址";
    self.isSectionMode = NO;
    // Do any additional setup after loading the view.
    listArray = [NSMutableArray array];
//    [self refList];
}


-(void)setupData
{
    [super setupData];
    
    for (NSDictionary *address in listArray) {
        YMParameterCellObj *obj = [[YMParameterCellObj alloc] initWithObjType:YMParameterCellObjTypeLabel];
        UILabel *label = (UILabel *)obj.accessoryView;
        label.textAlignment = NSTextAlignmentLeft;
        label.font = [UIFont systemFontOfSize:16];
        label.backgroundColor = [UIColor clearColor];
        obj.arrangementType = YMParameterCellArrangementTypeVertical;
        obj.title = address[@"consignee"];
        obj.value =  address[@"address"];;
        obj.cellHeigth =60;
        obj.userInfo =address;
        obj.cellAction = @selector(editAddress:);
        if([address[@"isdefault"] boolValue])
        {
            obj.accessoryType = UITableViewCellAccessoryCheckmark;
            
        }
        else{
        obj.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        [self.cellObjs addObject:obj];
    }
    
    
  YMParameterCellObj *obj = [[YMParameterCellObj alloc] initWithObjType:YMParameterCellObjTypeLabel];

    obj.title = @"新增地址";
    obj.cellHeigth =45;
    obj.headImage = [UIImage imageNamed:@""];
    obj.imageSize = CGSizeMake(30, 30);
    obj.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    obj.cellAction = @selector(editAddress:);
    
    [self.cellObjs addObject:obj];
    
    
    [self.tableView reloadData];

}

-(void)editAddress:(YMParameterCellObj *)obj
{
    MyAddressEditViewController *editViewController = [[MyAddressEditViewController alloc] init];
    [editViewController setAddress:obj.userInfo];
    [self.navigationController pushViewController:editViewController animated:YMParameterCellObjTypeTextField];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Request

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self refList];
}

- (BOOL)refList {
   
        if ([super startRequest]) {
            NSMutableDictionary * dic = [NSMutableDictionary dictionary];
            [dic setObject:[BSEngine currentEngine].user.uid forKey:@"uid"];
            [client requestFor:dic methodName:@"User/Api/getMyaddress"];
        }

    return YES;
}

- (BOOL)requestDidFinish:(id)sender obj:(NSDictionary *)obj {
    if ([super requestDidFinish:sender obj:obj]) {
        id data = [obj getArrayForKey:@"data"];
         [listArray removeAllObjects];
        [listArray addObjectsFromArray:data];
        [self setupData];
    
    }
    return NO;
}



@end
